package com.yigerende.web.admin;

import com.yigerende.model.Type;
import com.yigerende.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/*
* 分类页面，增删改查
* */
@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @GetMapping("/types")
    //@PageableDefault(size = 10,sort = {"id"},direction = Sort.Direction.DESC)
    // size一页放多少条数据,sort根据id排序，direction = Sort.Direction.DESC按照倒序排序(这样新增的都会显示在第一条)
    public String types(@PageableDefault(size = 10,sort = {"id"},direction = Sort.Direction.DESC)
                                    Pageable pageable, Model model){

        //分页方法listType
        //把查询到的一页数据存放到model模型里面，显示到网页上,前端引用page就可以显示出来了
        model.addAttribute("page",typeService.listType(pageable));
        return "admin/types";
    }




    //新增分类页面
    @GetMapping("/types/input")
    public String input(){
        return "admin/types-input";
    }




    //修改页面
    //点击编辑按钮，传递id和Type放到model传递过去
    @GetMapping("/types/{id}/input")
    public String editInput(@PathVariable Long id, Model model){
        //查询的值存放在type里面，前端直接用模板语法th:value="*{type.name}"type里面属性就可以了
        model.addAttribute("type",typeService.getType(id));
        return "admin/types-input";
    }


    //删除页面
    //点击编辑按钮，传递id和Type放到model传递过去
    @GetMapping("/types/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        typeService.deleteType(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/types";
    }




    //新增
    @PostMapping("/types1")
    public String post(@Valid Type type, RedirectAttributes attributes){
        //把用户输入的传递数据库查询有没有这个名字
        Type type1 = typeService.getTypeByName(type.getName());
        if(type1 != null){//判断用户输入的名字数据库有没有
            attributes.addFlashAttribute("message","对不起，操作失败,已经有这个分类!");
            return "redirect:/admin/types/input";
        }

        Type t = typeService.saveType(type);
        if(t == null){
            attributes.addFlashAttribute("message","对不起，操作失败!");
            return "redirect:/admin/types/input";
        }else{
            attributes.addFlashAttribute("message","恭喜操作成功");
            return "redirect:/admin/types";
        }

    }



    //修改
    @PostMapping("/types1/{id}")
    public String editPost(@Valid Type type, RedirectAttributes attributes,@PathVariable Long id){
        Type type1 = typeService.getTypeByName(type.getName());
        if(type1 != null){//判断用户输入的名字数据库有没有
            attributes.addFlashAttribute("message","对不起，操作失败,已经有这个分类!");
            return "redirect:/admin/types/{id}/input";
        }

        Type t = typeService.updateType(id,type);
        if(t == null){
            attributes.addFlashAttribute("message","对不起，更新失败!");
            return "redirect:/admin/types/input";
        }else{
            attributes.addFlashAttribute("message","恭喜更新成功");
            return "redirect:/admin/types";
        }

    }





}


